﻿@model Model.SysConfig
@{
    ViewBag.Title = "用户列表";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var opts = {
            rows: 10,
            height: 450,
            url: "",
            para: { roleID: UrlArray["roleID"] },
            idField: "Guid",
            checkbox: false,
            columns: [
                     { field: 'EmployeeName', title: '姓名', width: 15, sortable: true },
                     { field: 'UserName', title: '用户名', width: 20, sortable: true },
                     { field: 'OfficeEmail', title: '邮箱', width: 25 },
                     { field: 'OfficeTel', title: '电话', width: 20 },
                     { field: 'UserStatus', title: '状态', width: 8, align: 'center', sortable: true,
                         formatter: function (value, rowData, rowIndex) {
                             if (value == 1)
                                 return "启用";
                             else
                                 return "禁用";
                         }
                     },
                     { field: 'Guid', title: '操作', width: 12, align: 'center',
                         formatter: function (value, rowData, rowIndex) {
                             var doActionOptions = "<select onchange='return DoAction(this.options.selectedIndex,\"" +
                             value + "\",\"" + rowData.UserStatus + "\")' style='width: 70px; '><option value=''>-请选择-</option>";
                             if (rowData.UserStatus == 1)
                                 doActionOptions = doActionOptions + "<option>禁用</option>";
                             else
                                 doActionOptions = doActionOptions + "<option>启用</option>";
                             if (UrlArray["roleID"] == "3b46ae04-0152-483a-8ec4-5fa35edfbff2" && rowData.UserName == "SuperAdmin") {
                                 return doActionOptions + "</select>";
                             } else
                                 return doActionOptions + "<option>删除</option></select>";
                         }
                     }
                    ]

        };
        //“操作”下拉框事件
        function DoAction(i, value, s) {
            switch (i) {
                case 1:
                    UpUserStatus(value, s); // 启用禁用
                    break;
                case 2:
                    removeUserFromRole(value); // 删除
                    break;
            }
        }
        $(function () {
            InitList();
        });
        function UpUserStatus(id, s) {
            $.post("/Role/UpUserStatus", { guid: id, status: s }, function (res) {
                alert(res);
                InitList();
            });
        }
        function btnAdd_Click() {
            AddUser();
        }
        function btnSearch_Click() {
            var employeeName = $.trim($("#txtEmployeeName").val());
            var username = $.trim($("#txtUserName").val());
            var postUrl = "/Role/GetUserByRoleID/?employeeName=" + encodeURI(employeeName)
                + "&username=" + encodeURI(username);
            InitList(postUrl);
        }
        function InitList(postUrl) {
            if (typeof (postUrl) == "undefined" || postUrl.length == 0) {
                postUrl = "/Role/GetUserByRoleID";
            }
            opts.url = postUrl;
            $("#DataGrid").dg(opts);
        }
        function AddLocalUser(v) {
            showModalMany('AddContact', '/SystemConfiguration/UserList', '900', '500', addLocalUserToRole);
        }
        function addLocalUserToRole(v) {
            var userArr = new Array();
            for (var i = 0; i < v.length; i++) {
                userArr.push(v[i].Guid);
            }
            addUserToRole(userArr);
        }
        function AddModelUser(v) {
            showModalMany('Info', '/ModelConfig/GetModelData?checkbox=1&modelID=' + '@Model.KeyValue', '900', '400', addUserToRole);
        }
        function addUserToRole(userIDArray) {
            var userArr = new Array();
            for (var i = 0; i < userIDArray.length; i++) {
                userArr.push(userIDArray[i]);
            }
            if (userArr.length == 0) {
                return;
            }
            var roleID = $.getUrlParam("roleID");
            var postUrl = "/Role/AddUserToRole";
            var postData = { roleID: roleID, userIDs: userArr.join(","), modelID: '@Model.KeyValue' };
            $.ajax({
                type: "POST",
                async: true,
                url: postUrl,
                data: postData,
                success: function (result) {
                    if (result.Success == true) {
                        alert(result.Message);
                    }
                    else {
                        alert(result.Message);
                    }
                    InitList();
                }
            });
        }

        function removeUserFromRole(guid) {
            if (confirm("确定删除吗？")) {
                var postUrl = "/Role/RemoveUserFromRole";
                var postData = { guid: guid };
                $.ajax({
                    type: "POST",
                    async: true,
                    url: postUrl,
                    data: postData,
                    success: function (result) {
                        if (result.Success == true) {
                            alert(result.Message);
                        }
                        else {
                            alert(result.Message);
                        }
                        InitList();
                    }
                });
            }
        }

    </script>
}
<div style="margin: 5px;">
    @{ 
        var dataSearch = new ViewDataDictionary();
        dataSearch.Add("PanelTitle", "用户列表");
        @Html.Partial("_SearchTitlePanel", dataSearch); 
    }
    <table class="searchArea" width="100%">
        <tr>
            <td>
                姓名：
                <input id="txtEmployeeName" name="EmployeeName" type="text" class="inputSearch" style="width: 120px;" />
                用户名：
                <input id="txtUserName" name="UserName" type="text" class="inputSearch" style="width: 120px;" />
                <input id="btnSearch" type="button" class="button" value=" 搜 索 " onclick="btnSearch_Click()" />
            </td>
        </tr>
    </table>
</div>
<div style="margin: 5px; min-height: 300px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="300">
                用户列表
            </td>
            <td align="right">
                <input type="button" value=" 从本地新增 " class="button" onclick="AddLocalUser()" />
                <input type="button" value=" 从模型新增 " class="button" onclick="AddModelUser()" />
            </td>
        </tr>
    </table>
    <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
    </table>
    <div>
        <div id="Pagination" class="pagination">
        </div>
        <div id="loading" class="loading">
            <img src="/Images/tree_loading.gif" />
        </div>
    </div>
    <div id="msg">
    </div>
</div>
